/* eslint-disable vue/no-unused-vars */
<template>
  <div>
    <bread>
      <span slot="second">商品管理</span>
      <span slot="third">商品列表</span>
    </bread>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCate">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格区 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <!-- 是否有效模版 -->
        <template v-slot:isok="scope">
          <i class="el-icon-success" style="color:lightgreen" v-if="!scope.row.cat_deleted"></i>
          <i class="el-icon-error" style="color:red" v-else></i>
        </template>
        <!-- 排序模版-->
        <template v-slot:rank="scope">
          <!-- 一级 -->
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <!-- 二级数 -->
          <el-tag type="success" size="mini" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <!-- 三级 -->
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>

        <template slot="opearation">
          <!-- v-slot:opearation="scope" -->
          <el-button type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[3, 7, 10, 15]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 添加分类对话框 -->

    <el-dialog title="添加分类" :visible.sync="addCatedialogVisible" width="50%" @close="addCateClose">
      <!-- 添加分类表单 -->
      <el-form :model="addCateForm" :rules="addCateRules" ref="addCateRef" label-width="100px" >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" >
            <!-- options 指定数据源头  props 指定配置对象 -->
          <el-cascader
            v-model="selectedId"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Bread from '../../base/breadcrumb'
export default {
  components: {
    bread: Bread
  },
  data() {
    return {
      queryinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 7
      },
      cateList: [],
      total: 0,
      // table指定列定义
      columns: [
        {
          label: '分类名词',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          //   指定模版列
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'rank'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opearation'
        }
      ],
      addCatedialogVisible: false,
      addCateForm: {
        cat_name: '',
        // 分类id
        cat_pid: 0,
        // 分类等级
        cat_level: 0
      },
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCateList: [],
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
        // multiple: true
      },
      selectedId: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryinfo
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品数据失败')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange(newsize) {
      console.log(newsize)
      this.queryinfo.pagesize = newsize
      this.getCateList()
    },
    handleCurrentChange(newpage) {
      console.log(newpage)
      this.queryinfo.pagenum = newpage
      this.getCateList()
    },
    showAddCate() {
      this.getParentCateList()
      this.addCatedialogVisible = true
    },
    // 获取父级分类列表
    async getParentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类失败')
      }
      this.$message.success('获取父级分类成功')
      this.parentCateList = res.data
    },
    parentCateChange() {
      const len = this.selectedId.length
      if (len > 0) {
        this.addCateForm.cat_pid = this.selectedId[len - 1]
        this.addCateForm.cat_level = len
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    addCateConfirm() {
      console.log(this.addCateForm)
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        console.log(res)
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        this.getCateList()
        this.addCatedialogVisible = false
      })
    },
    addCateClose() {
      this.$refs.addCateRef.resetFields()
      this.selectedId = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader{
    width: 100%;
}

</style>
